import type {
  BookAddRequest,
  BookQueryRequest,
  BookUpdateRequest,
  ChapterQueryRequest,
  ChapterVo,
  DeleteRequest,
  PageBookVo,
  PageChapterVo,
} from '#/api/types';

import { requestClient } from '#/api/request';

/**
 * 获取用户所有菜单
 */
export async function getBooksByLoginUser(param: BookQueryRequest) {
  return requestClient.post<PageBookVo>('/book/my/list/page/vo', param, {
    withCredentials: true,
  });
}

export function addBook(param: BookAddRequest) {
  return requestClient.post<number>('/book/add', param, {
    withCredentials: true,
  });
}

export function updateBook(param: BookUpdateRequest) {
  return requestClient.post<boolean>('/book/update', param, {
    withCredentials: true,
  });
}

export function deleteBook(param: DeleteRequest) {
  return requestClient.post<boolean>('/book/delete', param, {
    withCredentials: true,
  });
}

export function getChapterOfBook(param: ChapterQueryRequest) {
  return requestClient.post<PageChapterVo>('/chapter/list/page/vo', param, {
    withCredentials: true,
  });
}

export function loadContentByChapterId(param: ChapterQueryRequest) {
  return requestClient.post<ChapterVo>(
    '/chapter/getContentByChapterId',
    param,
    {
      withCredentials: true,
    },
  );
}

export async function insertContentOfFile(file: File, bookId: number) {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('bookId', bookId.toString());

  return requestClient.post<boolean>('/book/insertContentOfFile', formData, {
    headers: {
      'Content-Type': 'multipart/form-data',
    },
    withCredentials: true,
  });
}
